package com.ruoyi.system.manager.impl;


import cn.hutool.core.collection.CollUtil;
import com.ruoyi.common.core.domain.entity.SysPost;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.manager.AdminManager;
import com.ruoyi.system.service.ISysPostService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

/**
 * admin管理类
 * @author zhoudong
 */
@Service
public class AdminManagerImpl implements AdminManager {

    @Value("${admin.deptId}")
    private String adminDeptId;

    @Value("${admin.serviceDeptId}")
    private String serviceDeptId;

    @Resource
    private ISysPostService postService;
    @Resource
    private ISysUserService userService;

    /**
     * 当前登录人是否是管理部门
     * @return
     * @param loginUser
     */
    @Override
    public boolean loginUserIsAdminDept(LoginUser loginUser) {
    	if (loginUser.getUser()==null) {
			return false;
		}
        String deptId = String.valueOf(loginUser.getUser().getDeptId());

        // 当前登录人是否是管理员部门
        if (StringUtils.isNotBlank(adminDeptId)) {
            String[] admins = adminDeptId.split(",", -1);
            for (String adId : admins) {
                if (adId.equals(deptId)) {
                    return Boolean.TRUE;
                }
            }
        }

        return Boolean.FALSE;
    }

    /**
     * 是否是管理者
     * @param loginUser
     * @return
     */
    @Override
    public boolean loginUserIsLeader(LoginUser loginUser) {
        SysUser user = loginUser.getUser();
        AtomicReference<Boolean> isLeader = new AtomicReference<>(Boolean.FALSE);
        // 是否是管理岗位
        if (user.isAdmin()) {
            isLeader.set(Boolean.TRUE);
        } else {
            if (CollUtil.isEmpty(loginUser.getPosts())) {
                return Boolean.FALSE;
            }
            loginUser.getPosts().parallelStream().forEach(post -> {
                isLeader.set(post.getPostCode().contains("leader"));
            });
        }
        return isLeader.get();
    }

    /**
     * 当前登录人是否是 客服部门
     * @param loginUser
     * @return
     */
    @Override
    public boolean loginUserIsServiceDept(LoginUser loginUser) {
        if (loginUser.getUser()==null) {
            return false;
        }
        String deptId = String.valueOf(loginUser.getUser().getDeptId());
        if (StringUtils.isNotBlank(serviceDeptId)) {
            String[] serviceDeptIds = serviceDeptId.split(",", -1);
            for (String sdId : serviceDeptIds) {
                if (sdId.equals(deptId)) {
                    return Boolean.TRUE;
                }
            }
        }

        // 当前登录人是否是 客服部门
        return Boolean.FALSE;
    }
    /**
     * 当前登录人是不是质检
     */
    @Override
    public boolean loginUserIsBranchBoss(LoginUser loginUser) {
        SysUser user = loginUser.getUser();
        AtomicReference<Boolean> isBoss = new AtomicReference<>(Boolean.FALSE);
        // 是否是管理岗位
        if (user.isAdmin()) {
            isBoss.set(Boolean.TRUE);
        } else {
            loginUser.getPosts().parallelStream().forEach(post -> {
                isBoss.set(post.getPostCode().contains("boss"));
            });
        }
        return isBoss.get();
    }

    /**
     * 2023-12-14 当前登录人是否是 公司管理员
     * @param loginUser
     * @return
     */
    @Override
    public boolean loginUserIsMchBoss(LoginUser loginUser) {
        SysUser user = loginUser.getUser();
        AtomicReference<Boolean> isBoss = new AtomicReference<>(Boolean.FALSE);
        // 是否是管理岗位
        if (user.isAdmin()) {
            isBoss.set(Boolean.TRUE);
        } else {
            loginUser.getPosts().parallelStream().forEach(post -> {
                isBoss.set(post.getPostCode().contains("leader_in_bo"));
            });
        }
        return isBoss.get();
    }

    /**
     * 登录人是否是 老师
     * <p>Title: loginUserIsTeacher</p>   
     * <p>Description: </p>   
     * @param loginUser
     * @return   
     * @see AdminManager#loginUserIsTeacher(LoginUser)
     */
	@Override
	public boolean loginUserIsTeacher(LoginUser loginUser) {
		SysUser user = loginUser.getUser();
        AtomicReference<Boolean> isLeader = new AtomicReference<>(Boolean.FALSE);
        // 是否是管理岗位
        if (user.isAdmin()) {
            isLeader.set(Boolean.TRUE);
        } else {
            loginUser.getPosts().parallelStream().forEach(post -> {
                isLeader.set(post.getPostCode().contains("teacher"));
            });
        }
        return isLeader.get();
	}

    @Override
    public boolean loginUserIsBranchCheckBoss(LoginUser loginUser) {

        AtomicReference<Boolean> isBoss = new AtomicReference<>(Boolean.FALSE);
        // 是否是质检岗位
        loginUser.getPosts().parallelStream().forEach(post -> {
            isBoss.set(post.getPostCode().contains("check_boss"));
        });
        return isBoss.get();
    }

    /**
     * 构建LoginUser信息
     * @param userId
     * @return
     */
    @Override
    public LoginUser buildLoginUserByUserId(Long userId) {

        LoginUser loginUser = new LoginUser();
        //部门集合
        List<SysPost> posts = postService.selectPostsByUserId(userId);
        loginUser.setPosts(posts);
        loginUser.setPostId(posts.get(0).getPostId());
        SysUser sysUser = userService.selectUserById(userId);
        loginUser.setUser(sysUser);

        return loginUser;

    }
}
